package gietkien;

import java.util.Random;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import ObjectGame.ConKien;
import ObjectGame.ImageStatic;

import com.nokia.mid.ui.DirectUtils;

public class GietKien extends Canvas implements Runnable {

	boolean run = false;
	MainPage main;
	int wid, hei;
	int toftlef = Graphics.TOP | Graphics.LEFT;
	Image imgBackground, imgbtnBack, imgbtnPlay;
	ConKien kien[];
	Random ran;
	int score = 0;
	int live = 10;
	boolean gameover = false;

	protected void hideNotify() {
		// TODO Auto-generated method stub
		run = false;
	}

	protected void showNotify() {
		// TODO Auto-generated method stub
		run = true;
	}

	public GietKien(MainPage main) {
		this.main = main;
		setFullScreenMode(true);
		init();
		khoidong();
	}

	public void init() {
		ran = new Random();
		wid = getWidth();
		hei = getHeight();
		try {
			imgBackground = Image.createImage("/background.png");
			imgbtnBack = Image.createImage("/btnback.png");
			imgbtnPlay = Image.createImage("/btnplay.png");
		} catch (Exception e) {
		}
		kien = new ConKien[5];
		int tocdo, toadoy;
		for (int i = 0; i < 5; i++) {
			tocdo = 1 + ran.nextInt(3);
			toadoy = ran.nextInt(20) - 20;
			kien[i] = new ConKien(50 * i + 5, toadoy, tocdo);
		}
	}

	public void khoidong() {
		int tocdo, toadoy;
		for (int i = 0; i < 5; i++) {
			tocdo = 1 + ran.nextInt(3);
			toadoy = ran.nextInt(20) - 20;
			kien[i].setX(50 * i + 5);
			kien[i].setY(toadoy);
			kien[i].setTocdo(tocdo);
		}
		live = 10;
		score = 0;
		gameover = false;

	}

	private void gameover(Graphics g) {
		g.setColor(0xffffff);
		g.fillRect(0, 0, wid, hei);
		g.drawImage(imgBackground, 0, 0, toftlef);
		g.drawImage(imgbtnBack, 0, 360, toftlef);
		g.drawImage(imgbtnPlay, 142, 360, toftlef);
		g.setColor(0x0000ff);
		drawString(g,"Your score: "+score, 40,200,20);
		g.drawImage(ImageStatic.gameovimg, 20, 100, toftlef);
	}

	protected void paint(Graphics g) {
		 if (gameover)
		 {
		gameover(g);
		 return;
		 }
		 g.setColor(0xffffff);
		 g.fillRect(0, 0, wid, hei);
		 g.drawImage(imgBackground, 0, 0, toftlef);
		
		 //ve con kien
		 for(int i=0;i<5;i++){
		 kien[i].draw(g);
		 }
		
		 //ve nut score
		 g.drawImage(ImageStatic.scoreimg,0,0, toftlef);
		 //ve diem
		 g.setColor(0xffffff);
		 drawString(g,"Score: "+score,5,2,15);
		 drawString(g,"Lives: "+live,160,2,15);
		 //ve nut back va play
		 g.drawImage(imgbtnBack, 0,360, toftlef);
		 g.drawImage(imgbtnPlay, 142,360, toftlef);
	}

	protected void pointerPressed(int x, int y) {
		// TODO Auto-generated method stub
		// neu cham nut submit
		if (!gameover) {
			for (int i = 0; i < 5; i++) {
				if (kien[i].kiemtracham(x, y))
					score += 5;
			}
		}
		if (y > getHeight() - 40) {
			if (x < 100 && y > getHeight() - 40) {
				main.hienthilish();
			} else
			// new game
			if (x > getWidth() - 120 && y > getHeight() - 40) {

				main.newgame();
			}
			return;
		}

	}

	public void update() {
		for (int i = 0; i < 5; i++) {
			kien[i].update();
			if (kien[i].isAnbanh()) {
				live -= 1;
				kien[i].setAnbanh(false);
			}
		}
		if ( live <= 0)
			gameover = true;

	}

	public void ht(String s) {
		System.out.println(s);
	}

	void drawString(Graphics g, String xau, int x, int y,int size) {

		Font font = DirectUtils.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, size);
		g.setFont(font);
		g.drawString(xau, x, y, Graphics.TOP | Graphics.LEFT);
	}

	public void start() {
		run = true;
		Thread t = new Thread(this);
		t.start();

	}

	public void stop() {
		run = false;
	}

	public void run() {
		// TODO Auto-generated method stub
		while (true) {
			if (run) {
				try {
					Thread.sleep(10);
					update();
					repaint();
				} catch (Exception e) {

				}
			}
		}

	}

}
